package chapter.third.item11;

import java.io.Serializable;

import com.alibaba.fastjson.JSON;

public class HashTable implements Cloneable{
	//散列桶数组
	private Entry[] buckets;

	public Entry[] getBuckets() {
		return buckets;
	}

	private static class Entry{
		/**
		 * 
		 */
		protected final Object key;
		protected Object value;
		protected Entry next;
		public Entry(Object key, Object value, Entry next) {
			this.key = key;
			this.value = value;
			this.next = next;
		}
		
		
		
		public Object getKey() {
			return key;
		}



		public Object getValue() {
			return value;
		}



		public Entry getNext() {
			return next;
		}



		Entry deepCopy(){
			Entry result = new Entry(key,value,next);
			for(Entry p = result;p.next!=null;p=p.next){
				p.next = new Entry(p.next.key,p.next.value,p.next.next);
			}
			return result;
		}
	}
	@Override
	protected HashTable clone() throws CloneNotSupportedException {
		HashTable table = (HashTable)super.clone();
		table.buckets = new Entry[buckets.length];
		for(int i=0;i<buckets.length;i++){
			if(buckets[i]!=null)
				table.buckets[i] = buckets[i].deepCopy();
		}
		return table;
	}
	
	public static void main(String[] args) {
		HashTable table = new HashTable();
		table.buckets = new Entry[16];
		table.buckets[2] = new HashTable.Entry("dfsa", "sfd", null);
		System.out.println(JSON.toJSONString(table));
	}
	
	
}	
